<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\Agent;
use App\Logic\AgentLogic;


class AgentController extends BaseController
{
    
	public function index(Request $request){
		$where[] = ['company_state','=',1];
		if ($request->input('categoryId')) {
			$where[] = ['class_id', '=', $request->input('categoryId')];
		}
		if ($request->input('nameLike')) {
			$where[] = ['company_name', 'like', "%{$request->input('nameLike')}%"];
		}
        //$agents = $agent->paginate(15);

		$agents = AgentLogic::getAgentList($where);
		
    	return $this->view(null,compact('agents'));
    }
    
    
    
    public function create()
    {   	
    	return view('admin.agent.create');
    }
    
    /**
     * @param AdminRequest $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(Request $request)
    {
    	
    	// 参数校验
    	$validator = Validator::make($request->all(),
    			[
    					'company_name' => 'required',
    					'address' => 'required',
    					'company_type' => 'required',
    					'legal_person' => 'required',
    			]
    	);
    	
    	$agentLogicModel = new AgentLogic();
    	$agentLogicModel->create($request->all());
    	  	
    	flash('添加代理商成功')->success()->important();
    	
    	return redirect()->route('agent.index');
    }
    
    
    /**
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id)
    {
    	$admin = $this->adminsService->ById($id);
    	
    	$roles = $this->rolesRepository->getRoles();
    	
    	return view('admin.admins.edit', compact('admin','roles'));
    }
    
    /**
     * @param AdminRequest $request
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(Request $request,$id)
    {
    	$this->adminsService->update($request,$id);
    	
    	flash('更新资料成功')->success()->important();
    	
    	return redirect()->route('admins.index');
    }
    
    /**
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function delete($id)
    {
    	$admin = $this->adminsService->ById($id);
    	
    	if(empty($admin))
    	{
    		flash('删除失败')->error()->important();
    		
    		return redirect()->route('admins.index');
    	}
    	
    	
    	$admin->roles()->detach();
    	
    	$admin->delete();
    	
    	
    	flash('删除成功')->success()->important();
    	
    	return redirect()->route('admins.index');
    }
    
    /**
     * @param $status
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function status($status,$id)
    {
    	$admin = $this->adminsService->ById($id);
    	
    	if(empty($admin))
    	{
    		flash('操作失败')->error()->important();
    		
    		return redirect()->route('admins.index');
    	}
    	
    	$admin->update(['status'=>$status]);
    	
    	flash('更新状态成功')->success()->important();
    	
    	return redirect()->route('admins.index');
    }
    
    
}
